package com.tea.room.service.tea.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tea.room.dao.tea.HouseInfoDao;
import com.tea.room.dao.tea.TeaOrderDao;
import com.tea.room.model.tea.HouseInfo;
import com.tea.room.model.tea.TeaOrder;
import com.tea.room.service.tea.HouseInfoService;
import com.tea.room.util.ConstantUtil;

@Service
@Transactional
public class HouseInfoServiceImpl implements HouseInfoService {

	@Autowired
	private HouseInfoDao houseInfoDao;
	
    @Autowired
    private TeaOrderDao teaOrderDao;
	
    private static Logger logger = LoggerFactory.getLogger(HouseInfoServiceImpl.class);
	
	@Override
	public Map<String, Object> findHouseInfo(String roomId, String openId) {
		Map<String,Object> map = new HashMap<String, Object>();
		if(roomId == null || roomId.equals("")){
			map.put("flag", ConstantUtil.ERROR_FLAG);
			map.put("message", "roomId 为空！");
			return map;
		}
		logger.info("传入的   openId:" + openId );
		
		try {
		    HouseInfo houseInfo = new HouseInfo();
		    houseInfo.setTeaCode(roomId);
            HouseInfo info = houseInfoDao.query(houseInfo);
            map = arrangement(info);
            TeaOrder order = new TeaOrder();
            order.setOpenId(openId);
            order.setPayStatus("Y");
            if(openId != null && !openId.equals("undefined") && !openId.equals("null") && openId != ""){
                List<TeaOrder> teaOrder = teaOrderDao.queryOrderByOpenid(order);
                if(teaOrder.size() > 0){
                    map.put("old_customers", "N");
                }else{
                    map.put("old_customers", "Y");
                }
            }
            map.put("flag", ConstantUtil.SUCCESS_FLAG);
        } catch (Exception e) {
            map.put("flag", ConstantUtil.ERROR_FLAG);
            map.put("message", "系统异常！");
            e.printStackTrace();
        }
		return map;
	}
	
	
	public Map<String, Object> arrangement(HouseInfo info){
	    List<String> list = new ArrayList<String>();
	    Map<String, Object> map = new HashMap<String, Object>();
	    list.add(info.getBanner1());
	    list.add(info.getBanner2());
	    list.add(info.getBanner3());
	    list.add(info.getBanner4());
	    list.add(info.getBanner5());
	    map.put("banner", list);
	    map.put("address", info.getAddress());
	    map.put("name", info.getName());
	    map.put("phone", info.getPhone());
	    map.put("price", info.getPrice());
	    map.put("sales_no", info.getSales_no());
	    map.put("teaCode", info.getTeaCode());
	    return map;
	}

}
